package pers.sports.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.sports.entity.Manager;
import pers.sports.service.ManagerService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员相关
 *
 * @datetime 2021/4/26 17:23
 */

@RestController
@RequestMapping("/manager")
public class ManagerController {

    private final ManagerService managerService;

    public ManagerController(ManagerService managerService) {
        this.managerService = managerService;
    }

    @RequestMapping(value = "/finall")
    public Map finall(Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        List<Manager> list = managerService.finall(page, limit);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", managerService.countNumber());
        map.put("data", list);
        return map;
    }

    @RequestMapping("/insert")
    public String insertManager(@RequestBody JSONObject jsonObject) {
        Boolean exist = managerService.findNumByManagerNum(jsonObject.getString("managerNum"));
        if (exist) {
            Manager m = new Manager();
            m.setManagerName(jsonObject.getString("managerName"));
            m.setManagerNum(jsonObject.getString("managerNum"));
            Boolean p = managerService.insertManager(m);
            if (p) {
                return "success";
            } else {
                return "新建失败";
            }
        } else {
            return "当前账号已存在，请核对";
        }

    }

    @RequestMapping("delete")
    public String deleteById(String id){
        return managerService.deleteById(id)?"success":"err";
    }
}
